package com.chatroom.dto;

import com.chatroom.model.Message;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MessageDTO {
    private Long id;
    private String content;
    private String senderUsername;
    private String receiverUsername;
    private String roomCode;
    
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    private LocalDateTime timestamp;
    
    private boolean groupMessage;
    
    public MessageDTO(Message message) {
        this.id = message.getId();
        this.content = message.getContent();
        this.senderUsername = message.getSender().getUsername();
        if (message.getReceiver() != null) {
            this.receiverUsername = message.getReceiver().getUsername();
        }
        if (message.getChatRoom() != null) {
            this.roomCode = message.getChatRoom().getRoomCode();
        }
        this.timestamp = message.getTimestamp();
        this.groupMessage = message.isGroupMessage();
    }
    
    // 使用字符串设置时间戳
    public void setTimestamp(String timestampStr) {
        if (timestampStr != null && !timestampStr.isEmpty()) {
            try {
                // 记录原始输入
                System.out.println("原始时间戳字符串: " + timestampStr);
                
                // 处理JavaScript ISO字符串，去除可能的毫秒部分
                if (timestampStr.contains(".")) {
                    int dotIndex = timestampStr.lastIndexOf(".");
                    int zIndex = timestampStr.indexOf("Z", dotIndex);
                    // 如果有Z结尾
                    if (zIndex > 0) {
                        String millisPart = timestampStr.substring(dotIndex + 1, zIndex);
                        if (millisPart.length() > 3) {
                            millisPart = millisPart.substring(0, 3);
                        }
                        timestampStr = timestampStr.substring(0, dotIndex + 1) + millisPart + "Z";
                    } else {
                        // 如果没有Z结尾
                        String millisPart = timestampStr.substring(dotIndex + 1);
                        if (millisPart.length() > 3) {
                            millisPart = millisPart.substring(0, 3);
                        }
                        timestampStr = timestampStr.substring(0, dotIndex + 1) + millisPart;
                    }
                }
                
                // 记录处理后的字符串
                System.out.println("处理后的时间戳字符串: " + timestampStr);
                
                // 尝试使用不同格式解析
                try {
                    // 尝试解析带Z的ISO格式
                    if (timestampStr.endsWith("Z")) {
                        this.timestamp = LocalDateTime.parse(
                            timestampStr.replace("Z", ""), 
                            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
                        );
                    } else {
                        // 尝试直接解析不带Z的格式
                        this.timestamp = LocalDateTime.parse(
                            timestampStr, 
                            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
                        );
                    }
                } catch (Exception e1) {
                    try {
                        // 尝试没有毫秒的格式
                        this.timestamp = LocalDateTime.parse(
                            timestampStr.endsWith("Z") ? timestampStr.replace("Z", "") : timestampStr, 
                            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
                        );
                    } catch (Exception e2) {
                        // 最后尝试标准格式
                        this.timestamp = LocalDateTime.parse(timestampStr);
                    }
                }
                
                System.out.println("解析后的LocalDateTime: " + this.timestamp);
            } catch (Exception e) {
                System.err.println("时间戳解析失败: " + e.getMessage());
                // 如果解析失败，使用当前时间
                this.timestamp = LocalDateTime.now();
                System.out.println("使用当前时间: " + this.timestamp);
            }
        }
    }
} 